﻿/* 
 * jQuery公用扩展方法
 */
jQuery.datetime = {
    Format: function (dt, fmt) {
        /// <summary>
        /// datetime类型显示格式转换
        /// </summary>
        /// <param name="dt">datetime类型</param>
        /// <param name="fmt">装换格式：y:年;M:月;d:日;h:小时;m:分;s:秒;q:季度;S:毫秒;</param>
        if (!dt) {
            return dt;
        }
        var dtstr = parseInt(dt.replace(/\D/igm, ""));
        var d = new Date(dtstr);
        var o = {
            "M+": d.getMonth() + 1,                      //月份   
            "d+": d.getDate(),                           //日   
            "h+": d.getHours(),                          //小时   
            "m+": d.getMinutes(),                        //分   
            "s+": d.getSeconds(),                        //秒   
            "q+": Math.floor((d.getMonth() + 3) / 3),    //季度   
            "S": d.getMilliseconds()                     //毫秒   
        };
        if (/(y+)/.test(fmt))
            fmt = fmt.replace(RegExp.$1, (d.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt))
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    }
}
jQuery.htmlEncode = function (value) {
    /// <summary>
    /// html转义
    /// </summary>
    /// <param name="value">需要转换html的文本</param>
    return $('<div/>').text(value).html();
}
jQuery.htmlDecode = function (value) {
    /// <summary>
    /// html反转义
    /// </summary>
    /// <param name="value">html文本</param>
    return $('<div/>').html(value).text();
}
jQuery.arraytotree = function (data, option) {
    /// <summary>
    /// 集合转换树数据对象
    /// </summary>
    /// <param name="data">集合数据</param>
    /// <param name="option">配置项<br/>
    /// id:选中后的值
    /// text:选中后显示的文本
    /// parentId:上级节点的id
    /// creatNode:创建界节点时执行的方法(子节点的顺序i,子节点数据)
    /// </param>
    var opt = {
        id: "Id",
        text: "Name",
        parentId: "ParentId",
        creatNode: function (i, item) { }
    }
    $.extend(opt, option);
    var treeData = [], tmpMap = [];
    for (var i = 0; i < data.length; i++) {
        tmpMap[data[i][opt.id]] = data[i];
    }
    for (i = 0, l = data.length; i < l; i++) {
        if (typeof opt.creatNode === "function") {
            opt.creatNode(i, data[i]);
        }
        data[i]['text'] = data[i][opt.text];
        if (tmpMap[data[i][opt.parentId]] && data[i][opt.id] != data[i][opt.parentId]) {
            if (!tmpMap[data[i][opt.parentId]]['nodes'])
                tmpMap[data[i][opt.parentId]]['nodes'] = [];
            tmpMap[data[i][opt.parentId]]['nodes'].push(data[i]);
        } else {
            treeData.push(data[i]);
        }
    }
    return treeData;
}

String.prototype.format = function (args) {
    /// <summary>
    /// 字符串格式化
    /// </summary>
    /// <param name="data">填充格式的内容，可以是数组或json对象</param>
    var result = this;
    if (arguments.length > 0) {
        if (arguments.length == 1 && typeof (args) == "object") {
            for (var key in args) {
                if (args[key] != undefined) {
                    var reg = new RegExp("({" + key + "})", "g");
                    result = result.replace(reg, args[key]);
                }
            }
        }
        else {
            for (var i = 0; i < arguments.length; i++) {
                if (arguments[i] != undefined) {
                    var reg = new RegExp("({[" + i + "]})", "g");
                    result = result.replace(reg, arguments[i]);
                }
            }
        }
    }
    return result;
}